using Hospital.API.Dto;
using Hospital.API.HospitalServices.IServices;
using Hospital.Domain;
using Hospital.ErrorCode;
using Microsoft.AspNetCore.Mvc;

namespace Hospital.API.Controllers
{
    /// <summary>
    /// 开单记录控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller][action]")]
    public class OrderRecordController : ControllerBase
    {
        private readonly IOrderRecordServices _orderRecordServices;

        public OrderRecordController(IOrderRecordServices orderRecordServices)
        {
            _orderRecordServices = orderRecordServices;
        }

        /// <summary>
        /// 获取开单记录列表
        /// </summary>
        /// <param name="queryDto">查询条件</param>
        /// <returns>开单记录列表</returns>
        [HttpGet]
        public async Task<APIResult<APIPaging<List<OrderRecord>>>> GetOrderRecordList([FromQuery] OrderRecordQueryDto queryDto)
        {
            return await _orderRecordServices.GetOrderRecordList(queryDto);
        }

        /// <summary>
        /// 获取开单记录详情
        /// </summary>
        /// <param name="id">开单记录ID</param>
        /// <returns>开单记录详情</returns>
        [HttpGet]
        public async Task<APIResult<OrderRecordDetailDto>> GetOrderRecordById(int id)
        {
            return await _orderRecordServices.GetOrderRecordById(id);
        }

        /// <summary>
        /// 创建开单申请
        /// </summary>
        /// <param name="createDto">创建开单申请DTO</param>
        /// <returns>创建结果</returns>
        [HttpPost]
        public async Task<APIResult<OrderRecord>> CreateOrderRecord([FromBody] CreateOrderRecordDto createDto)
        {
            return await _orderRecordServices.CreateOrderRecord(createDto);
        }

        /// <summary>
        /// 更新支付状态
        /// </summary>
        /// <param name="updateDto">更新支付状态DTO</param>
        /// <returns>更新结果</returns>
        [HttpPut]
        public async Task<APIResult<bool>> UpdatePaymentStatus([FromBody] UpdatePaymentStatusDto updateDto)
        {
            return await _orderRecordServices.UpdatePaymentStatus(updateDto);
        }

        /// <summary>
        /// 取消开单申请
        /// </summary>
        /// <param name="id">开单记录ID</param>
        /// <returns>取消结果</returns>
        [HttpPut]
        public async Task<APIResult<bool>> CancelOrderRecord(int id)
        {
            return await _orderRecordServices.CancelOrderRecord(id);
        }

        /// <summary>
        /// 获取院区列表
        /// </summary>
        /// <returns>院区列表</returns>
        [HttpGet]
        public async Task<APIResult<List<HospitalBranch>>> GetHospitalBranchList()
        {
            return await _orderRecordServices.GetHospitalBranchList();
        }

        /// <summary>
        /// 获取检查项目列表
        /// </summary>
        /// <param name="typeId">项目类型ID（可选）</param>
        /// <returns>检查项目列表</returns>
        [HttpGet]
        public async Task<APIResult<List<CheckItem>>> GetCheckItemList([FromQuery] int? typeId = null)
        {
            return await _orderRecordServices.GetCheckItemList(typeId);
        }

        /// <summary>
        /// 获取检查项目类型列表
        /// </summary>
        /// <param name="parentId">父级类型ID（可选）</param>
        /// <returns>检查项目类型列表</returns>
        [HttpGet]
        public async Task<APIResult<List<CheckItemType>>> GetCheckItemTypeList(int? parentId = null)
        {
            return await _orderRecordServices.GetCheckItemTypeList(parentId);
        }

        /// <summary>
        /// 获取已选项目的详细信息
        /// </summary>
        /// <param name="selectedItemIds">已选项目ID列表</param>
        /// <returns>已选项目的详细信息</returns>
        [HttpPost]
        public async Task<APIResult<List<SelectedItemDetailDto>>> GetSelectedItemsDetail([FromBody] List<int> selectedItemIds)
        {
            return await _orderRecordServices.GetSelectedItemsDetail(selectedItemIds);
        }

        /// <summary>
        /// 计算已选项目的总价
        /// </summary>
        /// <param name="selectedItems">已选项目列表</param>
        /// <returns>总价信息</returns>
        [HttpPost]
        public async Task<APIResult<OrderSummaryDto>> CalculateOrderSummary([FromBody] List<OrderItemDto> selectedItems)
        {
            return await _orderRecordServices.CalculateOrderSummary(selectedItems);
        }
    }
} 